package com.lgc.javathread.threadPoolTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/3/26.
 */
public class ThreadPoolProgram1 {
    public static void main(String[] args) {

        ExecutorService cachedThreadPool_1= Executors.newCachedThreadPool();
        ExecutorService cachedThreadPool_2= Executors.newSingleThreadExecutor();
        ExecutorService cachedThreadPool_3= Executors.newScheduledThreadPool(5);
        ExecutorService cachedThreadPool = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 100; i++) {
            MyRunnable2 myRun = new MyRunnable2(i);
            cachedThreadPool.execute(myRun);
        }

        System.out.println("isShutdown " + cachedThreadPool.isShutdown());
        System.out.println("isTerminated " + cachedThreadPool.isTerminated());
        cachedThreadPool.shutdown();
        System.out.println("isShutdown " + cachedThreadPool.isShutdown());

        try {
            cachedThreadPool.awaitTermination(5, TimeUnit.SECONDS);//等待5秒没完就继续走
            System.out.println("5秒没完就继续走！");
            System.out.println("=====================================");

            while (true)
            {
                if(cachedThreadPool.isTerminated())
                {
                    System.out.println("所有的子线程都结束了！");
                    break;
                }
                else {
                    System.out.println("执行中！");
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
